﻿using BLL.Entities;
using BLL.Repositories;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbFactory
{
    class ArticleFactory
    {
        internal static void Create()
        {
            //张三发布了8篇李四发布了4篇文章
            
            Article Z1 = publish(UserFactory.ZhangSan, "1", read("ZSone"), DateTime.Now.AddDays(1));
            Article Z2 = publish(UserFactory.ZhangSan, "2", "ww", DateTime.Now.AddDays(2));
            Article Z3 = publish(UserFactory.ZhangSan, "3", "ee", DateTime.Now.AddDays(3));
            Article Z4 = publish(UserFactory.ZhangSan, "4", "rr", DateTime.Now.AddDays(4));
            Article Z5 = publish(UserFactory.ZhangSan, "5", "tt", DateTime.Now.AddDays(5));
            Article Z6 = publish(UserFactory.ZhangSan, "6", "yy", DateTime.Now.AddDays(6));
            Article Z7 = publish(UserFactory.ZhangSan, "7", "uu", DateTime.Now.AddDays(7));
            Article Z8 = publish(UserFactory.ZhangSan, "8", "ii", DateTime.Now.AddDays(8));
            Article L1 = publish(UserFactory.LiSi, "1", "aa", DateTime.Now.AddDays(9));
            Article L2 = publish(UserFactory.LiSi, "2", "ss", DateTime.Now.AddDays(10));
            Article L3 = publish(UserFactory.LiSi, "3", "dd", DateTime.Now.AddDays(11));
            Article L4 = publish(UserFactory.LiSi, "4", "ff", DateTime.Now.AddDays(12));

        }
        private static string read(string name)
        {
            string path = Environment.CurrentDirectory.Replace("\\bin\\Debug", string.Empty);
            //Combine是组合路径
            return File.ReadAllText(Path.Combine(path, "articles", $"{name}.txt"));
        }
        private static Article publish(User user, string title, string description, DateTime time)
        {
            Article article = new Article
            {
                Author = user,
                Title = title,
                Description = description,
                PublishTime = time,
            };
            article.Publish();
            ArticleRepository articleRepository = new ArticleRepository(Global.GetDbContext());
            articleRepository.Save(article);
            return article;
        }
    }
}
